$(document).ready(function() {

    var email = null;
    var password = null;

    $("#post_btn").click(function() {

        if(!checkEmail()) {
            return;
        }
        if(!checkPassword()) {
            return;
        }
        submit();
    });

    function checkEmail() {

        email = $("#email_input").val();
        if(email == "") {
            alert("邮箱不能为空");
            return false;
        }
        return true;
    }

    function checkPassword() {

        password = $("#password_input").val();
        if(password == "") {
            alert("密码不能为空");
            return false;
        }
        return true;
    }

    function submit() {

        $.post('/user/registerPost', {
                email : email,
                password : password
            }, function(result) {
                if(!result) {
                    return;
                }
                var resultObj = JSON.parse(result);
                if(!resultObj) {
                    return;
                }
                var retCode = resultObj.retCode;
                if(retCode == 0) {
                    alert('注册失败');
                    var errCode = resultObj.result.errCode;
                    switch(errCode) {
                        case -2:
                            alert('邮箱已被注册');
                            console.log('submit()<<<---retCode != 1');
                            break;
                        case -3:
                            alert('邮件发送失败');
                            console.log('submit()<<<---retCode != 1');
                            break;
                    }
                } else if(retCode == 1) {
                    alert('账号验证激活邮件已经发送到您的邮箱！');
                    window.location.href="/user/login";
                }
            }, "text");
    }
});